import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_app/util/LogUtil.dart';

/**
 * https://book.flutterchina.club/chapter6/scroll_controller.html
    通过NotificationListener监听滚动事件和通过ScrollController有两个主要的不同：
    通过NotificationListener可以在从可滚动组件到widget树根之间任意位置都能监听。
    而ScrollController只能和具体的可滚动组件关联后才可以。
    收到滚动事件后获得的信息不同；NotificationListener在收到滚动事件时，
    通知中会携带当前滚动位置和ViewPort的一些信息，而ScrollController只能获取当前滚动位置。
 */

///
class ScrollNotificationWidget extends StatefulWidget {
  final String ?text;

  ScrollNotificationWidget({this.text}) : super();

  @override
  State<StatefulWidget> createState() {
    return _ScrollControllerState(text??"");
  }
}

class _ScrollControllerState extends State<ScrollNotificationWidget> {
  String ?title;

  _ScrollControllerState(String text) {
    title = text;
  }

  String _progress = "0%"; //保存进度百分比

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
          title??"",
          style: TextStyle(fontSize: 18),
        ),
      ),
      body: Scrollbar(
          child: NotificationListener<ScrollNotification>(
        // ignore: missing_return
        onNotification: (ScrollNotification notification) {
          double progress = notification.metrics.pixels /
              notification.metrics.maxScrollExtent;
          //重新构建
          setState(() {
            _progress = "${(progress * 100).toInt()}%";
          });
          print("BottomEdge: ${notification.metrics.extentAfter == 0}");
          return false; //放开此行注释后，进度条将失效
        },
        child: Stack(
          //Stack 相当于framlayout
          alignment: Alignment.center,
          children: <Widget>[
            ListView.builder(
                itemCount: 100,
                itemExtent: 50.0,
                itemBuilder: (context, index) {
                  return ListTile(title: Text("$index"));
                }),
            CircleAvatar(
              //显示进度百分比
              radius: 30.0,
              child: Text(_progress),
              backgroundColor: Colors.black54,
            )
          ],
        ),
      )),
    );
  }


}
